/**
 * 
 */
package com.easidea.tim.service.base;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

/**
 * 邮件发送处理Service，用于邮件发送处理。 实例初始化时自动装载邮件的各项配置参数，由外部调用者生成邮件正文以及接收人，调用本处理器发送邮件
 * 
 * @author Harrison
 * 
 */
public class EmailProcessService {

	private static final Logger log = Logger.getLogger(EmailProcessService.class);
	
	private JavaMailSender mailSender; //邮件发送器
	
	private String emailSubjectPrefix; //邮件标题前缀
	
	private String[] defaultReciptors; // 默认接收人
	
	/**
	 * 邮件发送
	 * @param message
	 */
	public void sendMail(final String message, final String subject, final String[] reciptors){
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws MessagingException {
            	if(reciptors != null && reciptors.length > 0) defaultReciptors = reciptors;
            	
            	if(defaultReciptors != null && defaultReciptors.length > 0){
            		List<InternetAddress> rcptAddrList = new ArrayList<InternetAddress>();
            		for(String addressStr : defaultReciptors){
            			if(StringUtils.isNotEmpty(addressStr)){
            				rcptAddrList.add(new InternetAddress(addressStr)); //添加邮件地址字符串
            			}
            		}
            		
            		if(rcptAddrList.size() > 0){
            			mimeMessage.setRecipients(Message.RecipientType.TO, rcptAddrList.toArray(new InternetAddress[]{}));
//                		mimeMessage.setFrom(new InternetAddress("harrison.yaog@gmail.com"));
                		mimeMessage.setSubject(emailSubjectPrefix + " - " +  subject);
                		mimeMessage.setText(message);
            		}else{
            			log.error("can not sent a email to non-reciptors !!");
            		}
            	}else{
            		log.error(" can not sent mail to non-reciptor list ~!");
            	}
            }
		};
		
        try{
            mailSender.send(preparator);
        }
        catch(MailException ex) {
        	log.error("send email failure ~! ", ex);
        	throw ex;
        }
	}

	/**
	 * @return the mailSender
	 */
	public JavaMailSender getMailSender() {
		return mailSender;
	}

	/**
	 * @param mailSender the mailSender to set
	 */
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * @return the emailSubjectPrefix
	 */
	public String getEmailSubjectPrefix() {
		return emailSubjectPrefix;
	}

	/**
	 * @param emailSubjectPrefix the emailSubjectPrefix to set
	 */
	public void setEmailSubjectPrefix(String emailSubjectPrefix) {
		this.emailSubjectPrefix = emailSubjectPrefix;
	}

	/**
	 * @return the defaultReciptors
	 */
	public String[] getDefaultReciptors() {
		return defaultReciptors;
	}

	/**
	 * @param defaultReciptors the defaultReciptors to set
	 */
	public void setDefaultReciptors(String[] defaultReciptors) {
		this.defaultReciptors = defaultReciptors;
	}

}
